package com.it.aop.demoaop01.jdbc.mapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.awt.print.PrinterAbortException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author: xiaoqiang
 * @Description:
 * @Date: 2021/6/25
 * @Version: 1.0
 */
@Service
public class UserMapperService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserMapper2 userMapper2;
    @Autowired
    private UserMapper3 userMapper3;
    @Autowired
    private UserMapper4 userMapper4;

    @Autowired
    private UserMapper5 userMapper5;

    @Autowired
    private UserMapper6 userMapper6;

    @Autowired
    JdbcTemplate jdbcTemplate;

    public void adduser() {
        Connection connection = null;
        try {
            connection = jdbcTemplate.getDataSource().getConnection();
            connection.setAutoCommit(false);
            userMapper.addDate();
            int a = 10 / 0;
            userMapper2.addDate();
            connection.commit();

        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }

    }

    public void adduser1() {
        Connection connection = null;
        try {
            //HikariProxyConnection@656844049 wrapping com.mysql.cj.jdbc.ConnectionImpl@72c9ebfa
            connection = jdbcTemplate.getDataSource().getConnection();
            connection.setAutoCommit(false);
            userMapper3.addDate(jdbcTemplate);
            int a = 10 / 0;
            userMapper4.addDate(jdbcTemplate);
            connection.commit();

        } catch (Exception e) {
            e.printStackTrace();

            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }

    /**
     * 解决方案1
     */
    public void adduser2() {
        Connection connection = null;
        try {
            //HikariProxyConnection@656844049 wrapping com.mysql.cj.jdbc.ConnectionImpl@72c9ebfa
            connection = jdbcTemplate.getDataSource().getConnection();
            connection.setAutoCommit(false);
            userMapper5.addDate(connection);
            int a = 10 / 0;
            userMapper6.addDate(connection);
            connection.commit();

        } catch (Exception e) {
            e.printStackTrace();

            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }
}
